package com.rw.service.impl;

import com.rw.dao.AccountDao;
import com.rw.pojo.Account;
import com.rw.service.AccountService;

import java.io.InputStream;
import java.sql.SQLException;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;


    public AccountDao getAccountDao() {
        return accountDao;
    }

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public boolean getCheckAccount(int aid, String apwd) throws SQLException {
//        调用dao进行查询
        Account pAccount = new Account();
        pAccount.setAid(aid);
        pAccount.setApwd(apwd);
        Account account = accountDao.select(pAccount);
        if (account != null){
            return true;
        }
        return false;
    }

    @Override
    public boolean getCheckMoney(Integer aid, Double money) throws SQLException {
//        boolean flag = false;
        //如果实现：
        //1.将金额查询出来以后，把金额和传入的参数进行比较
//        Account account = accountDao.getAccountByAid(aid);
//        if (account!=null){
//            if (account.getMoney() - money >= 0){
//                flag = true;
//            }
//        }
        //2.把金额和aid作为参数传入到sql中进行查询，如果查询到数据，那么表示金额正确，如果没有查询到，那么表示金额错误
        Account account = accountDao.getAccountByAidAndMoney(aid,money);
        System.out.println(account);
        return account != null;
    }

    @Override
    public boolean getCheckInAccount(Integer inUid, String inUname) throws SQLException {
        Account account = accountDao.getInAccount(inUid,inUname);
        return account != null;
    }

    @Override
    public boolean changeMoney(Integer aid, Double money, Integer inUid) throws SQLException {
        //如果进行操作
        //1.减少转账人账户的金额
        double money2 = money*-1;
        Account pAccount = new Account();
        pAccount.setAid(aid);
        pAccount.setMoney(money2);
        int num = accountDao.changeMoney(pAccount);
        //2.增加收款人账户的金额
        int num1 = 0;
        if (num == 1){
            Account pAccount2 = new Account();
            pAccount2.setMoney(money);
            pAccount2.setAid(inUid);
           num1 = accountDao.changeMoney(pAccount2);
        }

        if (num == 1){
            Account pAccount3 = new Account();
            pAccount3.setMoney(money);
            pAccount3.setAid(inUid+1);
            num1 = accountDao.changeMoney(pAccount3);
        }
        this.getMoney(aid,money,inUid);
        if (num1 != 1){
            throw new RuntimeException("123");
        }
        return num == 1 && num1 == 1;
    }

    @Override
    public boolean getMoney(Integer aid, Double money, Integer inUid) throws SQLException {
        //如果进行操作
        //1.减少转账人账户的金额
        double money2 = money*-1;
        Account pAccount = new Account();
        pAccount.setAid(aid);
        pAccount.setMoney(money2);
        int num = accountDao.changeMoney(pAccount);  //其他的数据库操作
        //2.增加收款人账户的金额
        int num1 = 0;
        if (num == 1){
            Account pAccount2 = new Account();
            pAccount2.setMoney(money);
            pAccount2.setAid(inUid);
            num1 = accountDao.changeMoney(pAccount2);
        }

        if (num == 1){
            Account pAccount3 = new Account();
            pAccount3.setMoney(money);
            pAccount3.setAid(inUid+1);
            num1 = accountDao.changeMoney2(pAccount3);
        }
        if (num1 != 1){
            throw new RuntimeException("123");
        }
        return num == 1 && num1 == 1;
    }


    @Override
    public Account getAccountByUid(Integer uid) throws SQLException {
        Account pAccount = new Account();
        pAccount.setUid(uid);
        return this.accountDao.select(pAccount);
    }
}
